package com.sjh.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sjh.service.IBag_service;
import com.sjh.vo.Bis_nurse_bag;
import com.sjh.vo.Bis_nurse_project;

@Controller
public class Bag_controller {
		@Autowired
		private IBag_service bag_service;
		
		int a=40000018;
		@RequestMapping("bagselect")
		public String bagselect(Map<String, Object> map,Bis_nurse_bag bag){
			List<Map> list = bag_service.bagselect(bag);
			map.put("list", list);
			return "sjh/selectBag";
		}
		
		
		@RequestMapping("addbag")
		public String addbag(Bis_nurse_bag bag,@RequestParam("file")MultipartFile[] file,HttpServletRequest request){
		
			a++;
			bag.setBag_id(a+"");
			
			String path=request.getRealPath("/upload");//tomcat/project
			File file1=new File(path);
			/*path=file1.getParent();//webapp
			path=path+"/upload/";*/
			Date date=new Date();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
			//path:c:/tools/tomcat/webapp/upload/20180719
			/*path=path+sdf.format(date);*/
			File dir=new File(file1.getPath(),sdf.format(date));
			if(!dir.exists()){
				//如果该目录不存在则创建目录
				dir.mkdirs();
			}
			File cover_photo=new File(dir.getPath(),file[0].getOriginalFilename());
			File cover_big_photo=new File(dir.getPath(),file[1].getOriginalFilename());
			try{
				if(!cover_photo.exists()){
					//此时创建之后只是一个空的文件
					cover_photo.createNewFile();
				}
				//将file中的内容复制到newfile中
				file[0].transferTo(cover_photo);
				file[1].transferTo(cover_big_photo);
			}catch(Exception e){
				e.printStackTrace();
			}
			
			bag.setCover_photo("/upload/"+dir.getName()+"/"+cover_photo.getName());
			bag.setCover_big_photo("/upload/"+dir.getName()+"/"+cover_big_photo.getName());
			
			
			
			bag_service.addbag(bag);
			return "redirect:/bagselect";
		}
		@RequestMapping("addinsert")
		public String add(HttpServletRequest request,Model model){
			//取出选择的id 用数组装起来
			String[] qByid = request.getParameterValues("checkbox");
			
			if (qByid!=null) {
				List<Bis_nurse_project> projects = new ArrayList<>();
				for (String project_id : qByid) {
					
					Bis_nurse_project project =  bag_service.selbyid(project_id);
					projects.add(project);
				}
				model.addAttribute("project",projects);
			}
			
			
			return "sjh/insertBag";
		}
		
		@RequestMapping("bagdel/{bag_id}")
		public String bagdel(@PathVariable String bag_id){
			bag_service.bagdel(bag_id);
			return "redirect:/bagselect";
		}
		
		//Ŀ���������
		@RequestMapping("bagupdByid/{bag_id}")
		public String bagupdByid(@PathVariable String bag_id,Model model){
			Bis_nurse_bag bag = bag_service.bagupdByid(bag_id);
			model.addAttribute("bag",bag);
			return "sjh/updateBag";
		}
		@RequestMapping("bagupdate")
		public String bagupdate(Bis_nurse_bag bag,@RequestParam("file")MultipartFile[] file,HttpServletRequest request){
			
			String path=request.getRealPath("/upload");//tomcat/project
			File file1=new File(path);
			/*path=file1.getParent();//webapp
			path=path+"/upload/";*/
			Date date=new Date();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
			if(file[0].getOriginalFilename()==null||file[0].getOriginalFilename()=="" || file[1].getOriginalFilename()==null||file[1].getOriginalFilename()=="" ){
				bag.setCover_photo(request.getParameter("photoname1"));
				bag.setCover_big_photo(request.getParameter("photoname2")); 
			   }
			
			else{
				 //path:c:/tools/tomcat/webapp/upload/20180719
					/*path=path+sdf.format(date);*/
					File dir=new File(file1.getPath(),sdf.format(date));
					if(!dir.exists()){
						//如果该目录不存在则创建目录
						dir.mkdirs();
					}
					File cover_photo=new File(dir.getPath(),file[0].getOriginalFilename());
					File cover_big_photo=new File(dir.getPath(),file[1].getOriginalFilename());
					try{
						if(!cover_photo.exists()){
							//此时创建之后只是一个空的文件
							cover_photo.createNewFile();
						}
						//将file中的内容复制到newfile中
						file[0].transferTo(cover_photo);
						file[1].transferTo(cover_big_photo);
					}catch(Exception e){
						e.printStackTrace();
					}
					
					bag.setCover_photo("/upload/"+dir.getName()+"/"+cover_photo.getName());
					bag.setCover_big_photo("/upload/"+dir.getName()+"/"+cover_big_photo.getName());
					 
				   
			   }
			
			
			bag_service.bagupdate(bag);
			return "redirect:/bagselect";
		}
		
		//添加护理
		@RequestMapping("addhuli")
		public String addhuli(Map<String, Object> map,Bis_nurse_project project,Model model,HttpServletRequest request){
			List<Map> hulilist = bag_service.addhuli(project);
			map.put("hulilist",hulilist);
			return "sjh/insertHuli";
		}
		
		
		//删除护理
		@RequestMapping("delecthuli/{project_id}")
		public String delecthuli(@PathVariable String project_id){
			bag_service.delecthuli(project_id);
			return "redirect:/insertBag";
		}
		
		//前台
				@RequestMapping("qtbagselect")
				public String qtbagselect(Map<String, Object> map, Bis_nurse_bag bag){
					List<Map> qtList = bag_service.qtbagselect(bag);
					map.put("qtlist", qtList);
					return "qtsjh/selectBag";
				}
				
		
		//详情
		@RequestMapping(value="bagupdByid",produces = "text/plain;charset=utf-8")
		@ResponseBody
		public String selectByid(String bag_id,HttpServletResponse response){
			String jsonStr = "";
			try {
				jsonStr = new ObjectMapper().writeValueAsString(bag_service.bagupdByid(bag_id));
				System.out.println(jsonStr);
			} catch (Exception e) {
			}
			return jsonStr;
		}

}
